<?php

namespace app\common\model\mall\shop;

use app\admin\controller\user\act\Message;
use think\Model;
use think\Db;
use think\Exception;

class Settlement extends Model
{
    // 表名
    protected $name = 'mall_shop_settlement';

    protected $pk = 'settlementId';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = "int";

    // 定义时间戳字段名
    protected $createTime = "createtime";
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'settlementType_text',
        'settlementStatus_text'

    ];

    public function getSettlementtypeList()
    {
        return ['1' => __('Settlementtype 1'),'0' => __('Settlementtype 0')];
    }

    public function getSettlementstatusList()
    {
        return ['1' => __('Settlementstatus 1'),'0' => __('Settlementstatus 0')];
    }


    public function getSettlementtypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['settlementType']) ? $data['settlementType'] : '');
        $list = $this->getSettlementtypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSettlementstatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['settlementStatus']) ? $data['settlementStatus'] : '');
        $list = $this->getSettlementstatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /**
     * 平台与商家即时结算
     */
    public function speedySettlement($orderId){
        $order = model('Order','model\mall\order')->get(['id'=>$orderId]);
        $shops = model('Shop','model\mall\shop')->get(['id'=>$order->shop_id]);
        if(empty($shops)) throw new Exception('结算失败，商家不存在');
        $data = [];
        $data['settlementType'] = 1;
        $data['shop_id'] = $order->shop_id;
        $data['settlementMoney'] = $order->scoreMoney + (($order->payType==1) ? $order->realTotalMoney : 0);
        $data['commissionFee'] = $order->commissionFee;
        $backMoney = $data['settlementMoney'] - $data['commissionFee'];
        $data['backMoney'] = $backMoney;
        $data['settlementStatus'] = 1;
        $data['settlementTime'] = date('Y-m-d H:i:s');
        $data['settlementNo'] = '';

        Db::startTrans();
        try{
            $settlementId = Db::name('mall_shop_settlement')->insertGetId($data);
            if($settlementId>0){
                $settlementNo =$settlementId.(fmod($settlementId,7));
                Db::name('mall_shop_settlement')->where('settlementId',$settlementId)->update(['settlementNo'=>$settlementNo]);
                $order->settlement_id = $settlementId;
                $order->save();

                //返还金额
                if($backMoney>0){
                    $remark = '结算订单申请【'.$settlementNo.'】返还金额¥'.$backMoney;
                    \app\common\model\mall\shop\Shop::money($backMoney,$order->shop_id,$remark);
                }

                //如果是货到付款并且有积分支付的话，还要补上一个积分支付的资金流水记录，不然流水上金额不对。
                if($order->payType==0 && $order->scoreMoney >0){
                    $remark = '结算订单申请【'.$settlementNo.'】返还积分支付金额¥'.$order->scoreMoney;
                    \app\common\model\mall\shop\Shop::money($order->scoreMoney,$order->shop_id,$remark);
                }

                //收取佣金
                if($order->commissionFee>0){
                    $remark = '结算订单申请【'.$settlementNo.'】收取订单佣金¥'.$order->commissionFee;
                    \app\common\model\mall\shop\Shop::money($order->commissionFee,$order->shop_id,$remark);
                }

                Db::commit();
                return $settlementId;

            }
        }catch (Exception $e) {
            Db::rollback();
            throw new Exception($e->getMessage());
        }
        throw new Exception('结算失败');
    }

}
